package com.club.football.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.club.football.entity.Events;
import com.club.football.entity.Schedule;
import com.club.football.entity.Team;
import com.club.football.mapper.EventsMapper;
import com.club.football.service.IntegralService;
import com.club.football.service.ScheduleService;
import com.club.football.service.TeamService;
import com.club.football.utils.BeanUtils;
import com.club.football.vo.IntegralVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.club.football.common.CommonConstants.MATCH_FILED;
import static com.club.football.common.CommonConstants.MATCH_SUCCESS;

/**
 * 实施集成服务
 *
 * @author 1
 * @date 2024/02/16
 */
@Service
public class IntegralServiceImpl implements IntegralService {

    @Resource
    private TeamService teamService;

    @Resource
    private ScheduleService scheduleService;

    @Resource
    private EventsMapper eventsMapper;

    @Override
    public List<IntegralVo> list(Long eventsId) {

        Events events = eventsMapper.selectById(eventsId);
        List<Schedule> scheduleList = scheduleService.lambdaQuery().eq(Schedule::getEventsId, eventsId).list();
        if (CollUtil.isEmpty(scheduleList)) {
            return new ArrayList<>();
        }
        //计算分数
        HashMap<Long, Integer> integralMap = new HashMap<>(10);
        for (Schedule schedule : scheduleList) {
            if (MATCH_SUCCESS.equals(schedule.getMatchSituation())) {
                matchSuccess(integralMap, schedule.getHomeTeamId());
                matchFailed(integralMap, schedule.getVisitingTeamId());
            } else if (MATCH_FILED.equals(schedule.getMatchSituation())) {
                matchSuccess(integralMap, schedule.getVisitingTeamId());
                matchFailed(integralMap, schedule.getHomeTeamId());
            } else {
                matchDraw(integralMap, schedule.getHomeTeamId());
                matchDraw(integralMap, schedule.getVisitingTeamId());
            }
        }
        Set<Long> teamIds = integralMap.keySet();
        List<Team> teamList = teamService.lambdaQuery().in(Team::getTeamId, teamIds).list();
        List<IntegralVo> res = teamList.stream().map(team -> {
            IntegralVo integralVo = BeanUtils.copier(team, new IntegralVo());
            integralVo.setEventId(eventsId);
            integralVo.setEventName(events.getName());
            integralVo.setIntegral(integralMap.get(team.getTeamId()));
            return integralVo;
        }).sorted(Comparator.comparing(IntegralVo::getIntegral).reversed()).collect(Collectors.toList());
        return res;
    }

    /**
     * 成功
     *
     * @param hashMap 哈希图
     * @param teamId  团队id
     */
    private void matchSuccess(HashMap<Long, Integer> hashMap, Long teamId) {
        Integer score = hashMap.getOrDefault(teamId, 0);
        hashMap.put(teamId, score + 3);
    }

    /**
     * 比赛抽签
     *
     * @param hashMap 哈希图
     * @param teamId  团队id
     */
    private void matchDraw(HashMap<Long, Integer> hashMap, Long teamId) {
        Integer score = hashMap.getOrDefault(teamId, 0);
        hashMap.put(teamId, score + 1);
    }

    private void matchFailed(HashMap<Long, Integer> hashMap, Long teamId) {
        Integer score = hashMap.getOrDefault(teamId, 0);
        hashMap.put(teamId, score);
    }
}
